package com.dhcc.bpm.modules.content.wrapper;

import com.dhcc.bpm.common.docNode.ForestDocNodeMerge;
import com.dhcc.bpm.common.cache.SysCache;
import com.dhcc.bpm.modules.content.entity.DocDirectory;
import com.dhcc.bpm.modules.content.vo.DocDirectoryVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName DocDirectoryWrapper
 * @Description  包装类,返回视图层所需的字段
 * @Author lijiacheng
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */

public class DocDirectoryWrapper extends BaseEntityWrapper<DocDirectory, DocDirectoryVO> {


    public static DocDirectoryWrapper build() {
        return new DocDirectoryWrapper();
    }

    @Override
    public DocDirectoryVO entityVO(DocDirectory docDirectory) {
        DocDirectoryVO directoryVO = BeanUtil.copy(docDirectory, DocDirectoryVO.class);
        assert directoryVO != null;
        if (Func.isEquals(docDirectory.getParentId(), String.valueOf(BladeConstant.TOP_PARENT_ID) )) {
            directoryVO.setParentName(BladeConstant.TOP_PARENT_NAME);
        }else{
            DocDirectory parent = SysCache.getDirectory(docDirectory.getParentId());
            directoryVO.setParentName(parent.getDocName());
        }
        return directoryVO;
    }


    public List<DocDirectoryVO> listDocNodeVO(List<DocDirectory> list) {
        List<DocDirectoryVO> collect = list.stream().map(docDirectory -> BeanUtil.copy(docDirectory, DocDirectoryVO.class)).collect(Collectors.toList());
        for (DocDirectoryVO i: collect){
            String nodeId = i.getDocCode();
            i.setNo(nodeId);
            i.setCode(nodeId);
            i.setParentNo(i.getParentId());
            i.setUrl(i.getUrl());
        }
        return ForestDocNodeMerge.merge(collect);
    }

}
